package com.bootcamp.javase.oop.extend;

/**
 * @program: ExtendsCode
 * @description: define a square class and a rectangular class
 * @author: Keyu Li
 * @create: 2021-08-31 19:48
 **/

public class TestQuad {
    public static void main(String[] args) {
        Square sq = new Square(1);
        sq.setLength(-1);
        sq.setLength(2);
        System.out.println("Length: " + sq.getLength()
                + " Perimeter: " + sq.getPerimeter()
                + " Size: " + sq.getSize());

        Rectangular rt = new Rectangular(1, 2);
        rt.setLength(-1);
        System.out.println("Length: " + rt.getLength()
                + " Width: " + rt.getWidth()
                + " Perimeter: " + rt.getPerimeter()
                + " Size: " + rt.getSize());
        rt.setLength(0);
        System.out.println("Length: " + rt.getLength()
                + " Width: " + rt.getWidth()
                + " Perimeter: " + rt.getPerimeter()
                + " Size: " + rt.getSize());
    }
}

class Square {
    private int length;

    public Square() {

    }

    public Square(int length) {
        this.length = length;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        if (length < 0) {
            System.out.println("边长不小于0！");
            return;
        } else
            this.length = length;
    }

    public int getPerimeter() {
        return 4 * this.length;
    }

    public int getSize() {
        return this.length * this.length;
    }
}

class Rectangular {
    private int width;
    private int length;

    public Rectangular() {

    }

    public Rectangular(int width, int length) {
        this.width = width;
        this.length = length;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        if (length < 0) {
            System.out.println("边长不小于0！");
            return;
        } else
            this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        if (width < 0) {
            System.out.println("边长不小于0！");
            return;
        } else
            this.width = length;
    }

    public int getPerimeter() {
        return 2 * (this.length + this.width);
    }

    public int getSize() {
        return this.length * this.width;
    }
}
